<template>
	<view class="products">
		<view class="search-bar">
			<view class="date" v-if="searchType === 0">
				<picker mode="date" :value="startDate" @change="changeDate">
					<text>{{startDate}}</text>
					<uni-icons type="calendar-filled" class="icon" color="#898989" size="18"></uni-icons>
				</picker>
			</view>
			<view class="date" v-else-if="searchType === 1">
				<picker mode="date" :value="startDate" @change="changeDate" fields="month">
					<text>{{startDate}}</text>
					<uni-icons type="calendar-filled" class="icon" color="#898989" size="18"></uni-icons>
				</picker>
			</view>
			<view class="date" v-else-if="searchType === 2">
				<picker mode="date" :value="startDate" @change="changeDate" fields="year">
					<text>{{startDate}}</text>
					<uni-icons type="calendar-filled" class="icon" color="#898989" size="18"></uni-icons>
				</picker>
			</view>
			<view class="tab-wrapper">
				<view class="item" :class="{'current': searchType === 0}" @click="selectDate(0)">
					日
				</view>
				<view class="item" :class="{'current': searchType === 1}" @click="selectDate(1)">
					月
				</view>
				<view class="item" :class="{'current': searchType === 2}" @click="selectDate(2)">
					年
				</view>
			</view>
		</view>
		<view class="tip">
			以下统计数字，不含退款退货
		</view>
		<view class="table-wrapper">
			<view class="thead-wrapper">
				<view class="th center" style="width: 100upx;">
					名次
				</view>
				<view class="th flex">
					<text>商品名称</text>
				</view>
				<view class="th" style="width: 130upx;" @click="beSort('salesVolume')">
					<text>销量</text>
					<image v-if="sort === 'salesVolume'" src="../static/image/sort.png" mode="aspectFill" class="icon">
					</image>
				</view>
				<view class="th" style="width: 150upx;" @click="beSort('salesAmount')">
					<text>货款</text>
					<image v-if="sort === 'salesAmount'" src="../static/image/sort.png" mode="aspectFill" class="icon">
					</image>
				</view>
				<!-- <view class="th" style="width: 140upx;" @click="beSort('serviceFee')">
					<text>服务费</text>
					<image v-if="sort === 'serviceFee'" src="../static/image/sort.png" mode="aspectFill" class="icon">
					</image>
				</view> -->
			</view>
			<view class="tbody-wrapper" v-if="list.length">
				<view class="item" v-for="(item,index) in list" :key="index" @click="goDetail(item)">
					<view class="tr">
						<view class="td center">
							{{ index + 1 }}、
						</view>
						<view class="td flex">
							<view class="productName">
								{{ item.productName }}
							</view>
						</view>
						<view class="td" style="width: 130upx;text-align: center;">
							{{ item.salesVolume }}件
						</view>
						<view class="td" style="width: 150upx;text-align: center;">
							{{ item.salesAmount }}元
						</view>
						<!-- <view class="td" style="width: 120upx;text-align: center;">
							{{ item.serviceFee }}元
						</view> -->
						<view class="td" style="width: 20upx;text-align: center;">
							<uni-icons type="right" size="16" color="#889999"></uni-icons>
						</view>
					</view>
				</view>
			</view>

			<view class="nodata-wrapper" v-else>
				<image src="https://sm.hobon.cc/static/hobon_noorder.png" class="data-image" mode="widthFix"></image>
				<view class="text">
					暂无数据~
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// import {
	// 	tableToExcel
	// } from '@/pages/util/exportExcel.js'

	export default {
		data() {
			return {
				searchType: 0, //0: 日  1: 月  2: 年
				startDate: this.$helper.getDay(0, '-'),
				endDate: this.$helper.getDay(0, '-'),
				range: [],
				end: this.$helper.getDay(0, '-'),
				list: [],
				sort: 'salesVolume', // 排序：销量salesVolume   销售额salesAmount 
			}
		},
		computed: {
			userInfo() {
				return this.$store.state.userInfo
			}
		},
		mounted() {
			this.getData()
		},
		onNavigationBarButtonTap() {
			this.exportExcel()
		},
		methods: {
			async getData() {
				const params = {
					pickupPointId: this.userInfo.pickupPointId
				}
				if (this.searchType === 0) {
					params.queryDay = this.startDate;
				} else if (this.searchType === 1) {
					params.queryMonth = this.startDate;
				} else if (this.searchType === 2) {
					params.queryYear = this.startDate;
				}
				const data = await this.$api.post('/app/statistic/pickupPointProductSalesReport', params)
				this.list = [];
				if (data.code === 200) {
					this.list = data.data;
					this.beSort('salesVolume')
				}
			},
			selectDate(type) {
				if (this.searchType === type) return
				this.searchType = type;
				if (type === 0) {
					this.startDate = this.$helper.getDay(0, '-')
				} else if (type === 1) {
					this.startDate = this.$helper.getDay(0, '-').substring(0, 7)
				} else if (type === 2) {
					this.startDate = this.$helper.getDay(0, '-').substring(0, 4)
				}
				this.getData()
			},
			changeDate(e) {
				this.startDate = e.detail.value;
				this.getData()
			},
			beSort(column) {
				this.sort = column;
				this.list.sort((a, b) => {
					return b[column] - a[column]
				})
			},
			goDetail(item) {
				uni.navigateTo({
					url: '/subPage_report/productDetail?productId=' + item.productId + '&date=' + this.startDate +
						"&searchType=" + this.searchType + "&productName=" + item.productName
				})
			},
			exportExcel() {
				if (!this.list.length) {
					uni.showToast({
						title: '暂无可导出数据',
						icon: 'none'
					})
					return
				}

				//要导出的json数据
				const tHead = ['序号', '货品名称', '规格', '采购量', '营业额贡献(元)']
				const jsonData = this.list.map((item, index) => {
					return {
						index: (index + 1),
						productName: item.productName,
						productSpecName: (item.productSpecName || '默认规格') + (
							item.productSpecPackageManner ? ('(' + item.productSpecQuantity + item
								.productSpecPackageManner +
								'/' + item.productSpecPackageUnit + ')') : '(散装)'
						),
						productQuantity: item.productQuantity,
						productAmount: item.productAmount
					}
				})

				let title = '商品采购报表';
				if (this.searchType === 1) {
					title = `${this.startDate.split('-')[0]}年${this.startDate.split('-')[1]}月-商品采购报表`
				} else if (this.searchType === 0) {
					title = `${this.startDate}-商品采购报表`
				} else {
					title = `${this.startDate}至${this.endDate}-商品采购报表`
				}
				//	tableToExcel(tHead, jsonData, title)
			},
		},
		components: {

		}
	}
</script>

<style lang="scss" scoped>
	.products {
		height: 100%;
		overflow: auto;

		.search-bar {
			height: 88upx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			background-color: #ffffff;
			margin-top: 2upx;
			padding: 0 20upx;

			.date {
				display: flex;
				align-items: center;
				color: #383838;
				font-size: 28upx;

				.icon {
					margin-left: 5upx;
				}
			}

			.tab-wrapper {
				display: flex;
				align-items: center;

				.item {
					padding: 5upx 24upx;
					color: #383838;
					background-color: #F2F4FA;
					border-radius: 30upx;
					margin-left: 20upx;
					font-size: 28upx;

					&.current {
						background-color: #F26022;
						color: #ffffff;
					}
				}
			}
		}

		.tip {
			padding-left: 30rpx;
			height: 60rpx;
			display: flex;
			align-items: center;
			font-size: 28upx;
			color: #666666;
		}

		.table-wrapper {
			.thead-wrapper {
				height: 88upx;
				display: flex;
				align-items: center;
				background-color: #ffffff;
				font-size: 26upx;

				.th {
					&.center {
						text-align: center;
					}

					&.flex {
						flex: 1;
					}

					.icon {
						width: 24upx;
						height: 24rpx;
					}
				}
			}

			.tbody-wrapper {
				.item {
					background-color: #ffffff;
					font-size: 26upx;
					margin: 10rpx 10rpx 0 10rpx;
					border-radius: 10rpx;
					padding: 20rpx 20rpx;
					box-sizing: border-box;
				}

				.tr {
					display: flex;
					align-items: center;

					&:first-child {
						margin-bottom: 10rpx;
					}
				}

				.td {
					&.center {
						text-align: center;
					}

					&.flex {
						flex: 1;
					}

					.icon {
						width: 24upx;
						height: 26rpx;
					}

					.productName {
						font-size: 26upx;
						color: #292929;
						line-height: 34upx;
						text-overflow: -o-ellipsis-lastline;
						overflow: hidden; //溢出内容隐藏
						text-overflow: ellipsis; //文本溢出部分用省略号表示
						display: -webkit-box; //特别显示模式
						-webkit-line-clamp: 2; //行数
						line-clamp: 2;
						-webkit-box-orient: vertical; //盒子中内容竖直排列
						width: 90%;
						padding-right: 30upx;
					}

					.productSpecName {
						color: #898989;
						font-size: 28upx;
					}
				}
			}
		}

		.nodata-wrapper {
			text-align: center;
			font-size: 32upx;
			color: #666666;

			.data-image {
				margin-top: 200upx;
				margin-bottom: 10upx;
				width: 296upx;
				height: 205upx;
			}
		}
	}
</style>